const Router = require("koa-router");
const router = new Router();

/**
 * 概览数据接口
 * 路径：GET /overview
 *
 * 返回数据结构：
 * [
 *   {
 *     title: "今日总收入(元)",
 *     amount: 123213,       // 金额
 *     trend: "increase",    // 趋势状态: "increase" 或 "decrease"
 *     percentage: 12.34     // 百分比数值
 *   },
 *   // ...其他数据项
 * ]
 */
router.get("/charging-station/overview", (ctx) => {
  try {
    // 定义标题列表
    const titles = [
      "今日总收入(元)",
      "本月总收入(万元)",
      "会员卡储存值金额(元)",
      "服务费总金额(元)",
      "停车费总金额(元)",
      "电费总金额(元)",
    ];

    // 生成随机数据
    const overviewData = titles.map((title) => {
      // 根据标题确定金额范围
      let minAmount, maxAmount;

      if (title.includes("今日总收入")) {
        minAmount = 50000;
        maxAmount = 300000;
      } else if (title.includes("本月总收入")) {
        minAmount = 1000; // 万元单位
        maxAmount = 5000; // 万元单位
      } else if (title.includes("会员卡")) {
        minAmount = 100000;
        maxAmount = 500000;
      } else if (title.includes("服务费")) {
        minAmount = 50000;
        maxAmount = 200000;
      } else if (title.includes("停车费")) {
        minAmount = 20000;
        maxAmount = 100000;
      } else {
        minAmount = 100000;
        maxAmount = 400000;
      }

      // 生成随机金额
      const amount =
        Math.floor(Math.random() * (maxAmount - minAmount + 1)) + minAmount;

      // 生成随机百分比（0-30%）
      const percentage = parseFloat((Math.random() * 30).toFixed(2));

      // 随机决定趋势（70%概率上涨，30%概率下跌）
      const trend = Math.random() > 0.3 ? "increase" : "decrease";

      return {
        title,
        amount,
        trend,
        percentage,
      };
    });

    // 返回成功响应
    ctx.body = {
      code: 200,
      success: true,
      message: "获取概览数据成功",
      data: overviewData,
    };
  } catch (error) {
    console.error("获取概览数据出错:", error);
    ctx.body = {
      code: 500,
      success: false,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});

// 导出路由
module.exports = router;
